package com.neuedu.mvc.controller;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;


//http://localhost:8080/web/req
@WebServlet(urlPatterns = "/req")
public class RequestAPIController extends HttpServlet {


    /**
     * 地址栏  get
     * form method=get
     * @param req   an {@link HttpServletRequest} object that
     *                  contains the request the client has made
     *                  of the servlet
     *
     * @param resp  an {@link HttpServletResponse} object that
     *                  contains the response the servlet sends
     *                  to the client
     *
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }


    /**
     *  * form method=post
     * @param request   an {@link HttpServletRequest} object that
     *                  contains the request the client has made
     *                  of the servlet
     *
     * @param response  an {@link HttpServletResponse} object that
     *                  contains the response the servlet sends
     *                  to the client
     *
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //request 
        String method = request.getMethod();
        System.out.println("method = " + method);


        //获取请求路径
        String requestURI = request.getRequestURI();
        StringBuffer requestURL = request.getRequestURL();
        System.out.println("requestURI = " + requestURI);
        System.out.println("requestURL = " + requestURL);


        String protocol = request.getProtocol(); //1.1
        String localName = request.getLocalName();
        int localPort = request.getLocalPort();

        System.out.println("protocol = " + protocol);
        System.out.println("localName = " + localName);
        System.out.println("localPort = " + localPort);


        String scheme = request.getScheme();
        String remoteHost = request.getRemoteHost();
        String remoteAddr = request.getRemoteAddr();
        String remoteUser = request.getRemoteUser();
        Integer remotePort = request.getRemotePort();
        System.out.println("scheme = " + scheme);
        System.out.println("remoteHost = " + remoteHost);
        System.out.println("remoteAddr = " + remoteAddr);
        System.out.println("remoteUser = " + remoteUser);
        System.out.println("remotePort = " + remotePort);

        String accept = request.getHeader("Accept");
        System.out.println("accept = " + accept);

        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()){
            String headerName = headerNames.nextElement();
            String headValue = request.getHeader(headerName);
            System.out.printf("headerName:%s , headValue:%s" , headerName,headValue.toString());
        }


    }
}
